from Tkinter import *
from tkSimpleDialog import *

class ProjectListDialog(Toplevel):
    """
    Represents the frame that provides a user interface to access a list of existing projects.
    """
    
    def __init__(self, parentControl, listener):
        Toplevel.__init__(self, parentControl)
        
        self.listener = listener
        
        label = Label(self, text="Projects:")
        label.grid(row = 0, column = 0, columnspan = 2)

        self.projectsListbox = Listbox(self)
        self.projectsListbox.bind("<Double-Button-1>", self._onOpen)
        self.projectsListbox.grid(row = 1, column = 0, columnspan = 2)
        
        button = Button(self, text="Annuler", command = self._onCancel)
        button.grid(row = 3, column = 0)
        
        button = Button(self, text="Ouvrir", command = self._onOpen)
        button.grid(row = 3, column = 1)
        
    def clearProjectNames(self):
        """
        Clears the list of project names displayed by this window.
        """
        self.projectsListbox.delete(0, END)
        
    def addProjectName(self, name):
        """
        Adds a project name to the list of project names displayed.
        """
        self.projectsListbox.insert(END, name)
        
    def getSelectedProjectName(self):
        """
        Gets the currently selected project name.
        """
        return self.projectsListbox.get(ACTIVE)
        
    def _onCancel(self):
        self.listener.onCancelProjectOpening()
        
    def _onOpen(self, dummy = None):
        self.listener.onOpenSpecificProject()